'use strict';
const{ DEFAULT_TYPE_LIST } = require('../config/constant')
const { typeTable, ObjectId } = require('../models/database_tables');

class TypeService  {
  async setDefaultType(user_id) {
    DEFAULT_TYPE_LIST.map(item => item.user_id = user_id)
    try {
      const result = await typeTable.save(DEFAULT_TYPE_LIST)
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
  async getList(user_id) {
    try {
      const result = await typeTable.where({user_id}).find()
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
  async delete(type_id) {
    try {
      let result = await typeTable.where({ _id: ObjectId(type_id) }).find()
      result = await typeTable.delete(result)
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
  async add(params) {
    try {
      let result = await typeTable.save(params)
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
}

module.exports = new TypeService();
